<!-- 首页秒杀热门 -->
<template>
    <div class="sdy-home-hot">
        <div class="top">
            <span>京东秒杀</span>
                <p>
                    <span>{{hours<10?'0'+hours:hours}}</span>:
                    <span>{{minutes<10?'0'+minutes:minutes}}</span>:
                    <span>{{seconds<10?'0'+seconds:seconds}}</span>
                </p>
            <span>按钮</span>
        </div> 
        <div class="content">
           <ul>
            <li
            v-for="item in hostList"
            :key="item.proid">
                <img :src="item.img1" alt="">
                <span>{{item.sales}}</span>
            </li>
           </ul>
        </div>
    </div>
</template>

<script setup lang="ts">
// 导入秒杀热门接口
import { hostListApi } from '@/api/goods'
import { ref, onMounted, watch } from 'vue'
import type { MyHotItem } from '@/config/interface'
import { Dialog } from 'vant';

const hostList = ref<MyHotItem[]>([])

// 秒杀倒计时时间逻辑（从服务器拿到时间戳）
const ms = ref<number>(123456)

// 开启定时器操作ms
const timer = setInterval(() => ms.value--, 1000)

// 准备小时、分钟、秒的变量
const hours = ref<number>(0)
const minutes = ref<number>(0)
const seconds = ref<number>(0)

// 监控ms变化，ms发生变化时，小时、分钟、秒都会变化
// 因为页面刚开始需要执行，
const watchHandler = () => { 
     // 换算
    hours.value = Math.floor(ms.value/(60*60))
    minutes.value = Math.floor(ms.value%(60*60)/60)
    seconds.value = ms.value % 60
    // 倒计时截止时，关闭定时器
    if (ms.value <= 0) {
        clearInterval(timer)
        Dialog.alert({title:'秒杀',message:'秒杀已经开始！'})
    }
}
watch(ms,watchHandler)
onMounted(async () => { 
    watchHandler()
    const res = await hostListApi({ limitNum: 10 })
    // console.log(res);
    
    hostList.value = res.data
    // console.log(res.data)
})
</script>

<style scoped lang="scss">
@import '@/styles/variable.scss';
.sdy-home-hot{
    height: 165px;
    box-sizing: border-box;
    padding: 15px;
    display: flex;
    flex-direction: column;
    // background-color: pink;
    background-clip: content-box;
    >.top{
        height: 50px;
        background: url() no-repeat;
        background-size: 100% 30px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        box-sizing: border-box;
        padding: 0 10px;
        >p{
            display: flex;
            margin-right: 150px;
            align-items: center;
            height: 30px;
            >span{
                background-color: $globalColor;
                padding: 2px 3px;
                font-size: 12px;
                color: white;
                margin: 0 2px;
                overflow: hidden;
                border-radius: 5px;
            }
        }
    }
    >.content{
        flex: 1;
        overflow-y: auto;

        //去掉滚动条
    &::-webkit-scrollbar{ width: 0}

        >ul{
            display: flex;
            height: 100%;
            background-color: white;
            >li{
                height: 100%;
                width: 50px;
                display: flex;
                justify-content: center;
                flex-direction: column;
                align-content: center;
                background-color: white;
                padding: 3px;
                background-clip: content-box;
                // border: 1px solid #333;
                margin: 0 3px;

                >img{
                    width: 45px;
                    height: 50px;
                    display: block;
                }
                >span{
                    color: red;
                    font-size: 12px;

                }
            }
        }
    }
}
</style>